package com.pig4cloud.pig.demo.Controller;

import com.pig4cloud.pig.common.core.util.R;
import com.pig4cloud.pig.common.security.annotation.Inner;
import com.pig4cloud.pig.demo.Service.PurchaseService;
import com.pig4cloud.pig.demo.Util.TokenUtils;
import com.pig4cloud.pig.demo.feign.Entity.Purchase;
import com.pig4cloud.pig.demo.feign.vo.PurchaseVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@Inner
public class PurchaseController {
    @Autowired
    private PurchaseService purchaseService;

    @GetMapping("/purchase/insertPurchase/{uid}/{aid}")
    public R insertPurchase(@PathVariable Long uid, @PathVariable Long aid) {
        Purchase purchase = new Purchase();
        purchase.setUId(uid);
        purchase.setAid(aid);

        int i = purchaseService.insertPurchase(purchase);
        if (i == 0)
            return R.failed();
        return R.ok();
    }

    /**
     * 根据用户id查询购物车信息
     * @param request
     * @return
     */
    @GetMapping("/purchase/list")
    //@Inner(value = false)
    public R getPurchaseList(HttpServletRequest request) {
        Long id = TokenUtils.getId(request);
        if (id == null) {
            return R.failed("token错误");
        }
        List<PurchaseVO> purchaseVOS = purchaseService.queryPurchaseList(id);
        return R.ok(purchaseVOS);
    }

    /**
     * 根据主键删除购物车中的商品
     * @param id
     * @return
     */
    @DeleteMapping("/purchase/{id}")
    public R deletePurchase(@PathVariable Long id){
        purchaseService.removeById(id);
        return R.ok();
    }

}
